View Javadoc
1   package org.apache.maven.surefire.common.junit48;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.booter.ProviderParameterNames;
23  import org.apache.maven.surefire.group.match.GroupMatcher;
24  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
25  import org.apache.maven.surefire.group.parse.ParseException;
26  import org.apache.maven.surefire.testset.TestListResolver;
27  import org.junit.runner.manipulation.Filter;
28  
29  import java.util.Map;
30  import java.util.Set;
31  
32  /**
33   * @author Todd Lipcon
34   */
35  public class FilterFactory
36  {
37      private final ClassLoader testClassLoader;
38  
39      public FilterFactory( ClassLoader testClassLoader )
40      {
41          this.testClassLoader = testClassLoader;
42      }
43  
44      public Filter createGroupFilter( Map<String, String> providerProperties )
45      {
46          String groups = providerProperties.get( ProviderParameterNames.TESTNG_GROUPS_PROP );
47          String excludedGroups = providerProperties.get( ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP );
48  
49          GroupMatcher included = null;
50          if ( groups != null && groups.trim().length() > 0 )
51          {
52              try
53              {
54                  included = new GroupMatcherParser( groups ).parse();
55              }
56              catch ( ParseException e )
57              {
58                  throw new IllegalArgumentException(
59                      "Invalid group expression: '" + groups + "'. Reason: " + e.getMessage(), e );
60              }
61          }
62  
63          GroupMatcher excluded = null;
64          if ( excludedGroups != null && excludedGroups.trim().length() > 0 )
65          {
66              try
67              {
68                  excluded = new GroupMatcherParser( excludedGroups ).parse();
69              }
70              catch ( ParseException e )
71              {
72                  throw new IllegalArgumentException(
73                      "Invalid group expression: '" + excludedGroups + "'. Reason: " + e.getMessage(), e );
74              }
75          }
76  
77          if ( included != null && testClassLoader != null )
78          {
79              included.loadGroupClasses( testClassLoader );
80          }
81  
82          if ( excluded != null && testClassLoader != null )
83          {
84              excluded.loadGroupClasses( testClassLoader );
85          }
86  
87          return new GroupMatcherCategoryFilter( included, excluded );
88      }
89  
90      public Filter createMethodFilter( String requestedTestMethod )
91      {
92          return new MethodFilter( requestedTestMethod );
93      }
94  
95      public Filter createMethodFilter( TestListResolver resolver )
96      {
97          return new MethodFilter( resolver );
98      }
99  
100     public Filter createFailingMethodFilter( Map<Class<?>, Set<String>> failingClassMethodMap )
101     {
102         return new FailingMethodFilter( failingClassMethodMap );
103     }
104 
105     public Filter and( Filter filter1, Filter filter2 )
106     {
107         return new AndFilter( filter1, filter2 );
108     }
109 }